//
//  ShopTabCell.m
//  RentTable
//
//  Created by xchuan xchuan on 2024/12/1.
//

#import "ShopTabCell.h"

@interface ShopTabCell()


@property (nonatomic, strong) UIButton *shopDeleteButton;
@property(nonatomic, strong) UIImageView *orderImageView;
@property (nonatomic, strong) UILabel *hotPriceLabel;
@property (nonatomic, strong) UILabel *hotPriceDayLabel;
@property (nonatomic, strong) UILabel *hotLabel;
@property (nonatomic, strong) UILabel *priceLabel;

@property (nonatomic, strong) UIButton *minusButton;
@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) UIButton *plusButton;

@property (nonatomic, assign) BOOL isSelect;

@end

@implementation ShopTabCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.isSelect = false;
        self.count = 1;
        [self setupUI];
    }
    return self;
}

- (void)setRentModel:(RentModel *)rentModel {
    _rentModel = rentModel;
    
    self.orderImageView.image = [UIImage imageNamed:rentModel.rentImage];
    self.hotPriceLabel.text = rentModel.rentTitle;
    self.hotPriceDayLabel.text = [NSString stringWithFormat:@"规格：［%@; %@; %ld个月]; ×%ld",rentModel.rentCondition,rentModel.rentColor,rentModel.rentPeriod,rentModel.rentCount];
    self.priceLabel.text = [NSString stringWithFormat:@"¥%.1f/天",rentModel.rentPrice];
}

- (void)setAllSelect:(BOOL)allSelect {
    _allSelect = allSelect;
    self.shopSelectButton.selected = allSelect;
}

- (void)setupUI {
    
    UIView *shopTabBackView = [UIView new];
    shopTabBackView.backgroundColor = myRGBHex(0xFFFFFF);
    shopTabBackView.layer.cornerRadius = 20;
    shopTabBackView.layer.masksToBounds = true;
    [shopTabBackView setUserInteractionEnabled:true];
    [self.contentView addSubview:shopTabBackView];
    [shopTabBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-20 * scaleH);
    }];
    
    self.shopSelectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shopSelectButton setBackgroundImage:[UIImage imageNamed:@"未勾选"] forState:UIControlStateNormal];
    [self.shopSelectButton setBackgroundImage:[UIImage imageNamed:@"勾选"] forState:UIControlStateSelected];
    [self.shopSelectButton addTarget:self action:@selector(shopSelectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [shopTabBackView addSubview:self.shopSelectButton];
    
    [self.shopSelectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    self.shopDeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shopDeleteButton setBackgroundImage:[UIImage imageNamed:@"删除"] forState:UIControlStateNormal];
    [self.shopDeleteButton addTarget:self action:@selector(shopDeleteButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [shopTabBackView addSubview:self.shopDeleteButton];
    [self.shopDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(14);
        make.trailing.mas_equalTo(-14);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    self.orderImageView = [UIImageView new];
    self.orderImageView.image = [UIImage imageNamed:@"1"];
    [shopTabBackView addSubview:self.orderImageView];
    self.orderImageView.layer.cornerRadius = 5;
    self.orderImageView.layer.masksToBounds = true;
    [self.orderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(48);
        make.top.mas_equalTo(18);
        make.size.mas_equalTo(CGSizeMake(85, 85));
    }];
    
    self.hotPriceLabel = [[UILabel alloc] init];
    self.hotPriceLabel.font = [UIFont pingFangSCMediumWithSize:14];
    self.hotPriceLabel.textColor = myRGBHex(0x333333);
    self.hotPriceLabel.text = @"台球桌标准型钢库中式黑八台球厅会所家用成";
    self.hotPriceLabel.textAlignment = NSTextAlignmentLeft;
    self.hotPriceLabel.numberOfLines = 0;
    [shopTabBackView addSubview:self.hotPriceLabel];
    [self.hotPriceLabel sizeToFit];
    [self.hotPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.leading.equalTo(self.orderImageView.mas_trailing).offset(12);
        make.trailing.mas_equalTo(-47);
    }];
    
    self.hotPriceDayLabel = [[UILabel alloc] init];
    self.hotPriceDayLabel.font = [UIFont pingFangSCRegularWithSize:12];
    self.hotPriceDayLabel.textColor = myRGBHex(0x999999);
    self.hotPriceDayLabel.textAlignment = NSTextAlignmentLeft;
    self.hotPriceDayLabel.text = @"规格：［全新；黑色；3个月；×1］";
    [shopTabBackView addSubview:self.hotPriceDayLabel];
    [self.hotPriceDayLabel sizeToFit];
    [self.hotPriceDayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hotPriceLabel.mas_bottom).offset(4);
        make.leading.equalTo(self.orderImageView.mas_trailing).offset(12);
        make.trailing.mas_equalTo(-17);
    }];
    
    
    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.font = [UIFont pingFangSCRegularWithSize:14];
    self.priceLabel.textColor = myRGBHex(0xF51D1D);
    self.priceLabel.textAlignment = NSTextAlignmentLeft;
    self.priceLabel.text = @"¥59.9/天";
    [shopTabBackView addSubview:self.priceLabel];
    [self.priceLabel sizeToFit];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hotPriceDayLabel.mas_bottom).offset(11);
        make.leading.equalTo(self.orderImageView.mas_trailing).offset(12);
        make.height.mas_equalTo(20);
    }];
    
    UIView *shopBackView = [UIView new];
    shopBackView.backgroundColor = myRGBHexAlpha(0xCCCCCC, 0.2);
    [shopBackView setUserInteractionEnabled:true];
    shopBackView.layer.cornerRadius = 10;
    shopBackView.layer.masksToBounds = true;
    [shopTabBackView addSubview:shopBackView];
    [shopBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.priceLabel.mas_centerY);
        make.trailing.mas_equalTo(-14);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(80);
    }];
    
    self.minusButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.minusButton setTitle:@"-" forState:UIControlStateNormal];
    self.minusButton.titleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    [self.minusButton setTitleColor:myRGBHex(0x999999) forState:UIControlStateNormal];
    [self.minusButton addTarget:self action:@selector(minusButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    

    self.countLabel = [[UILabel alloc] init];
    self.countLabel.text = [NSString stringWithFormat:@"%ld", (long)self.count];
    self.countLabel.font = [UIFont pingFangSCRegularWithSize:14];
    self.countLabel.textAlignment = NSTextAlignmentCenter;
    

    self.plusButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.plusButton setTitle:@"+" forState:UIControlStateNormal];
    [self.plusButton setTitleColor:myRGBHex(0x999999) forState:UIControlStateNormal];
    self.plusButton.titleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    [self.plusButton addTarget:self action:@selector(plusButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    

    [shopBackView addSubview:self.minusButton];
    [shopBackView addSubview:self.countLabel];
    [shopBackView addSubview:self.plusButton];
    
    [self.plusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-9);
        make.width.height.equalTo(@10);
    }];

    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_offset(0);
        make.trailing.equalTo(self.plusButton.mas_leading).offset(-10);
        make.width.equalTo(@20);
    }];
    
    [self.minusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_offset(0);
        make.trailing.equalTo(self.countLabel.mas_leading).offset(-10);
        make.width.height.equalTo(@10);
    }];
}

- (void)shopSelectButtonClicked:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.isSelect = sender.selected;
    
    if (self.selectBlock) {
        self.selectBlock(self.rentModel, self.isSelect);
    }
}

- (void)shopDeleteButtonClicked {
    if (self.deleteBlock) {
        self.deleteBlock(self.rentModel);
    }
}

- (void)minusButtonTapped {
    if (self.count <= 1) {
        return;
    }
    self.count--;
    self.countLabel.text = [NSString stringWithFormat:@"%ld", (long)self.count];
    if (self.countChanged) {
        self.countChanged(self.rentModel,self.count,self.isSelect);
    }
}

- (void)plusButtonTapped {
    self.count++;
    self.countLabel.text = [NSString stringWithFormat:@"%ld", (long)self.count];
    
    if (self.countChanged) {
        self.countChanged(self.rentModel,self.count,self.isSelect);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
